﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using System.Threading;
using DataAccess;
//using Common.Interfaces;
using SmartHouseWcfService;
using Network;


namespace Core
{
    public class SmartHouse
    {
        private CommonDataStorage m_commonDataStorage;
        private SmartHouseWcfService.SmartHouseWcfService wcfService;
        private DataAccessor dataAccessor;
        private WcfServiceHost host;
        private ExternalServiceManager extServManager;
        volatile bool m_exit;

        public SmartHouse()
        {
            m_commonDataStorage = new CommonDataStorage();
            m_exit = false;
            dataAccessor = new DataAccessor();
            wcfService = new SmartHouseWcfService.SmartHouseWcfService();
            wcfService.DataAccessor = dataAccessor;
        }

        public void Run()
        {

            
            SmartHouseDataSet dsSHouse = dataAccessor.Get();
            SmartHouseInitializer SHInitializer = new SmartHouseInitializer();
            m_commonDataStorage = SHInitializer.CreateDataStorage(dsSHouse);

            host = new WcfServiceHost();
            host.StartSrvice(dataAccessor);
            

            #region Old working code (commented)
            //IntegerVariable temperatureIntVariable = new IntegerVariable("temperature", "sdfdf", "18");
            //BooleanVariable conderBoolVariable = new BooleanVariable("conder", "ads", "false");
            //CommonDataStorage.Variables.Add(temperatureIntVariable);
            //CommonDataStorage.Variables.Add(conderBoolVariable);

            //Common.Variable tmp = CommonDataStorage.Variables.Find(
            //    delegate(Common.Variable v)
            //    {
            //        return v.Name == "temperature";
            //    }
            //    );
            //ConditionSet condSetForTemperature = new ConditionSet();
            //condSetForTemperature.ListOfConditions.Add(
            //    new Common.Condition(tmp, "20", ConditionType.More)
            //    );

            //List<Common.Variable> listToEvent = new List<Common.Variable>();
            //listToEvent.Add(tmp);
            //SmartHouseEvent whenLess20EnableCond = new SmartHouseEvent("whenMore20EnableCond", "sd",
            //    listToEvent, condSetForTemperature, true);


            //Common.Operation enablingConder = new Common.Operation("enablingConder", "asd", true);
            //Common.Action actionToEnableConder = new SetCommand("conder", "true");
            //enablingConder.AddAction(actionToEnableConder);

            //Common.Binding temperatureCoolerBinding = new Common.Binding(whenLess20EnableCond, enablingConder);

            //CommonDataStorage.Bindings.Add(temperatureCoolerBinding);
            //CommonDataStorage.Operations.Add(enablingConder);
            //CommonDataStorage.Events.Add(whenLess20EnableCond);


            // //now we should save all this elements into DB
            //List<IVariable> tmpList = new List<IVariable>();
            //foreach (var v in CommonDataStorage.Variables)// omg! Should I really do this?
            //{
            //    tmpList.Add((IVariable)v);
            //}

            #endregion

            #region Writing into DB(commented)

            #endregion

            Thread thread = new Thread(RunForThread);
            thread.Start();
            Thread.Sleep(200);

            // pulse to work
            extServManager= new ExternalServiceManager(m_commonDataStorage);
            extServManager.Start();
            //Common.Variable temperatureIntVariable1 = m_commonDataStorage.Variables.Find(// find event to perform changes
            //    delegate(Common.Variable v)
            //    {
            //        return v.Name == "temperature";
            //    });
            //((Common.IntegerVariable)temperatureIntVariable1).CurrentIntegerValue = 21;

        }

        public void Stop()
        {
            host.StopService();
            extServManager.Stop();
            m_exit = true;
        }

        private void RunForThread()
        {
            while (!m_exit)
            {
                foreach (Common.Variable v in m_commonDataStorage.Variables)
                {
                    if (v.IsChanged)
                    {
                        Logger.Instance().Record(v.Name + " : " + v.CurrentValue.ToString());
                        v.ConfirmChanges();     // start triggering process                  
                    }
                    Thread.Sleep(50);
                }
            }
        }

        
    }
}
